<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\TeachingAidsService;
use App\Services\TeachingAidService;
use App\Services\CollectionService;
use Session;

class TeachingAidsController extends Controller
{
    public function view(){
        $userinfo = Session::get('userinfo');
        if(empty($userinfo)){
            $_data = array('code'=>'3x000001','msg'=>'请登录');
        }else{
            $teachingAids = new TeachingAidsService;
            $teachingaid = new TeachingAidService;
            $collection = new CollectionService;

            $aidsInfo = $teachingAids->getTaInfo();
            if(!empty($aidsInfo)){
                $collectionInfo = $collection->getCollectionInfo($userinfo['id'],$aidsInfo->id,'realias');
                $aidsInfo->aidlist = $teachingaid->getAidList($aidsInfo->id);
                $_data = array('code'=>'2x000001','aidsInfo'=>$aidsInfo,'collectionInfo'=>$collectionInfo);
            }else{
                $_data = array('code'=>'4x000001','msg'=>'暂无教具包');
            }
        }
        return response()->json($_data);
    }

    public function addCollection(){
        $userinfo = Session::get('userinfo');
        if(empty($userinfo)){
            $_data = array('code'=>'3x000001','msg'=>'请登录');
        }else{
            date_default_timezone_set('PRC');
            $collection = new CollectionService;
            $data['member_id'] = $userinfo['id'];
            $data['type'] = $_POST['type'] ? addslashes($_POST['realia']) : 'realia';
            $data['product_id'] = intval($_POST['product_id']);
            $data['add_date'] = date('Y-m-d H:i:s');

            $collectioninfo = $collection->getCollectionInfo($userinfo['id'],$data['product_id'],$data['type']);
            if(!empty($collectioninfo)){
                $_data = array('code'=>'3x000001','msg'=>'商品已收藏');
            }else{
                $res = $collection->insertCollection($data);
                if($res){
                    $_data = array('code'=>'2x000001','msg'=>'收藏成功');
                }else{
                    $_data = array('code'=>'4x000001','msg'=>'收藏失败');
                }
            }
        }

        return response()->json($_data);
    }

    public function aidList($id){
        $userinfo = Session::get('userinfo');
        if(empty($userinfo)){
            $_data = array('code'=>'3x000001','msg'=>'请登录');
        }else{
            $teachingAid = new TeachingAidService;
            $aid_list = $teachingAid->getAidList($id);
            if(!empty($aid_list)){
                $_data = array('code'=>'2x000001','aid_list'=>$aid_list);
            }else{
                $_data = array('code'=>'4x000001','msg'=>'暂无教具');
            }
        }
        return response()->json($_data);
    }

    public function aidInfo($id){
        $userinfo = Session::get('userinfo');

        if(empty($userinfo)){
            $_data = array('code'=>'2x000001','msg'=>'请登录');
        }else{
            $teachingAid = new TeachingAidService;
            $collection = new CollectionService;
            $aidInfo = $teachingAid->getAidInfoById($id);
            $collectionInfo = $collection->getCollectionInfo($userinfo['id'],$id,'realia');
            if(empty($aidInfo)){
                $_data = array('code'=>'4x000001','msg'=>'暂无教具');
            }else{
                $_data = array('code'=>'2x000001','aidInfo'=>$aidInfo,'collectioninfo'=>$collectionInfo);
            }
        }
        return response()->json($_data);
    }
}
